package io.renren.modules.oa.controller.api;

import java.util.Date;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang.StringUtils;
import org.json.JSONArray;
import org.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;

import com.google.common.collect.Lists;
import com.google.common.collect.Maps;

import io.renren.common.utils.DateUtils;
import io.renren.common.utils.R;
import io.renren.common.utils.RedisUtils;
import io.renren.common.utils.dingtalk.DingtalkUtils;
import io.renren.modules.oa.entity.OaPlanConfigEntity;
import io.renren.modules.oa.entity.OaPlanEmployeeEntity;
import io.renren.modules.oa.redis.DingtalkRedis;
import io.renren.modules.oa.service.EscInstancesService;




@RestController
@RequestMapping("api/esc/instances")
public class ApiEscInstanceController {

	@Autowired
    private EscInstancesService escInstancesService;
	//https://weui.io/#preview
	//http://192.168.1.245/renren-admin/api/esc/instances/index
	@RequestMapping(value="/index") 
    public ModelAndView index(HttpServletRequest request, HttpServletResponse response){ 
		//String accessToken = "bf8c15b526d5371fb80b8b9361895b80";
		
		//request.setAttribute("list", getList(merchantId, months));
	/*	if(months < 0){
			request.setAttribute("one", "");
			request.setAttribute("two", "weui-bar__item_on");
		}else{
			request.setAttribute("one", "weui-bar__item_on");
			request.setAttribute("two", "");
		}
		*/
		List<Map> list = escInstancesService.queryEscInstanceList(1, 100);
		Long currTime = DateUtils.addDateMonths(new Date(), 1).getTime();	//时间戳  秒
		for (Map map : list) {
			String expired_time = map.get("expired_time").toString();
			long expired = DateUtils.stringToDate(expired_time, DateUtils.DATE_TIME_PATTERN).getTime();
			if(expired > currTime){
				map.put("warn", "");
			}else{
				map.put("warn", "warn");
			}
			
		}
		request.setAttribute("list", list);
		return new ModelAndView("web/esc/index"); 
    }
    
	/**
	 * http://192.168.1.245/renren-admin/api/esc/instances/query_esc_instance_list?pageNo=1&pageSize=1
	 * @param pageNo
	 * @param pageSize
	 * @return
	 */
	@RequestMapping(value = "query_esc_instance_list")
	public R query_esc_instance_list(@RequestParam(value="pageNo",defaultValue="1")Integer pageNo,
			@RequestParam(value="pageSize",defaultValue="20")Integer pageSize) {
		//验证新手机号 的 验证码
		List<Map> list = escInstancesService.queryEscInstanceList(pageNo, pageSize);
		if(list != null && list.size() > 0){
			return R.ok().put("l", list);
		}else{
			if(pageNo == 1){
				return R.error(103,"暂无数据");
			}else{
				return R.error(104,"暂无数据");
			}
		}
	}

}
